package springboot.Tools.JSONToSETime;

import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import springboot.Tools.Date.Month;
import springboot.Tools.Date.StartAndEndTime;
import springboot.Tools.Date.Week;

import java.text.SimpleDateFormat;

/**
 * wxWork
 * springboot.Tools.JSONToSETime
 * Created by Blue on 2021/9/6.
 */

@Data
public class JsonToSETime {
  private String options;
  private SwitchPeriod switchPeriod;
  private CustomTime customTime;

  public JsonToSETime(String options) {
    JSONObject jsonObject = JSONObject.parseObject(options);
    this.customTime = new CustomTime();
    if (jsonObject != null) {
      this.options = jsonObject.getString("options");
    }

    if (jsonObject == null) {
      Month month = StartAndEndTime.getMonth(0);
      this.customTime.setStartTime(month.getMonthStartTime());
      this.customTime.setEndTime(month.getMonthEndTime());
    } else if (this.options.equals("SwitchPeriod")) {
      this.switchPeriod = new SwitchPeriod();
      JSONObject switchPeriod1 = JSONObject.parseObject(jsonObject.getString("switchPeriod"));
      this.switchPeriod.setPeriod(switchPeriod1.getString("period"));
      if (this.switchPeriod.getPeriod().equals("month")) {
        this.switchPeriod.setMonth(switchPeriod1.getInteger("month"));
        Month month = StartAndEndTime.getMonth(this.switchPeriod.getMonth());
        this.customTime.setStartTime(month.getMonthStartTime());
        this.customTime.setEndTime(month.getMonthEndTime());
      } else if (this.switchPeriod.getPeriod().equals("week")) {
        this.switchPeriod.setWeek(switchPeriod1.getInteger("week"));
        this.switchPeriod.setMonth(switchPeriod1.getInteger("month"));
        Week week = StartAndEndTime.getWeek(this.switchPeriod.getMonth(), this.switchPeriod.getWeek());
        this.customTime.setStartTime(week.getWeekStartTime());
        this.customTime.setEndTime(week.getWeekEndTime());
      } else {
        Month month = StartAndEndTime.getMonth(0);
        this.customTime.setStartTime(month.getMonthStartTime());
        this.customTime.setEndTime(month.getMonthEndTime());
      }
    } else if (this.options.equals("CustomTime")) {
      JSONObject switchPeriod1 = JSONObject.parseObject(jsonObject.getString("customTime"));
      String startTime = switchPeriod1.getString("startTime");
      String endTime = switchPeriod1.getString("endTime");
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      try {
        this.customTime.setStartTime(sdf.parse(startTime));
        this.customTime.setEndTime(sdf.parse(endTime));
      } catch (Exception e) {
        System.err.println("JsonToSETime = " + e);
      }
      if (this.customTime.getStartTime() == null || this.customTime.getEndTime() == null) {
        Month month = StartAndEndTime.getMonth(0);
        this.customTime.setStartTime(month.getMonthStartTime());
        this.customTime.setEndTime(month.getMonthEndTime());
      }
    }
  }
}

